package dao

import (
	"context"
	"mall/internal/model"
	"mall/pkg/orm"
)

func (d *Dao) GetConfig(ctx context.Context, sgId uint32) (config *model.Config, err error) {
	config = &model.Config{}
	if err = d.TransContext(ctx).Take(config,
		"sg_id = ?", sgId).Error; err != nil {
		err = d.processSqlError(err)
	}
	return
}

// 查询是否符合代发条件
func (d *Dao) GetConfigByStoreId(ctx context.Context, sgId, storeId uint32) (config *model.Config, err error) {
	config = &model.Config{}
	if err = d.TransContext(ctx).Take(config,
		"sg_id = ? AND ANY('store_ids') = ?", sgId, storeId).Error; err != nil {
		err = d.processSqlError(err)
	}
	return
}

func (d *Dao) GetDecoration(ctx context.Context, sgId, pStoreId uint32, sourceType uint8) (
	decoration *model.Decoration, err error) {
	decoration = &model.Decoration{}
	if err = d.TransContext(ctx).Take(decoration,
		"sg_id = ? AND p_store_id = ? AND source_type = ?",
		sgId, pStoreId, sourceType).Error; err != nil {
		err = d.processSqlError(err)
	}
	return
}

func (d *Dao) GetDecorationTemplate(ctx context.Context, operationMode uint8, isHQ bool) (
	decorationInfo orm.JSON, err error) {
	decoration := &model.DecorationTemplate{}
	if err = d.TransContext(ctx).Take(decoration,
		"operation_mode = ? AND is_hq = ?",
		operationMode, isHQ).Error; err != nil {
		err = d.processSqlError(err)
	}
	decorationInfo = decoration.Decoration
	return
}
